package com.lan;


import com.lan.antlr.ScriptLexer;
import com.lan.antlr.ScriptParser;
import com.lan.evalulate.AstEvaluator;
import com.lan.parse.ParseJslListener;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.ParseTreeWalker;


public class ScriptCompiler {

    private ScriptContext context;

    private ScriptLexer lexer;

    private ScriptParser parser;

    public Object compile(String script) {
        context = new ScriptContext();
        //词法分析
        lexer = new ScriptLexer(CharStreams.fromString(script));
        CommonTokenStream tokens = new CommonTokenStream(lexer);

        //语法分析
        parser = new ScriptParser(tokens);
//        context.setAst(parser.variableDeclarator());
        context.setAst(parser.prog());

        //语义分析
        ParseTreeWalker walker = new ParseTreeWalker();

        //多步的语义解析。
        //优点：1.代码更清晰；2.允许使用在声明之前，这在支持面向对象、递归函数等特征时是必须的。
        //pass1：类型和Scope


//        TestScriptBaseListener scopeScanner = new TestScriptBaseListener(context);
//        walker.walk(scopeScanner, context.getAst());

        System.out.println(context.getAst().toStringTree(parser));
        ParseJslListener parseJslListener = new ParseJslListener(context);
        walker.walk(parseJslListener, context.getAst());

        AstEvaluator astEvaluator = new AstEvaluator(context);
        Object result = astEvaluator.visit(context.getAst());
        System.out.println("result:" + result);
//        return context;
        return result;
    }




}
